<template>
    <div class="sedu">
        <el-form :model="userinfo" :inline="true" label-width="80px">
            <el-form-item label="当前额度" label-width="80px">
                <el-input v-model="userinfo.quota" :disabled="disabled" />
            </el-form-item>
            <el-form-item label="额度有效期" label-width="120px">
                <el-input
                    v-model="userinfo.quota_end_time"
                    :disabled="disabled"
                />
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8" v-show="isshow">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                    v-hasPermi="['biz:edu:add']"
                    >新增</el-button
                >
            </el-col>
            <!--<el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:edu:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:edu:remove']"
        >删除</el-button>
      </el-col>-->
            <el-col :span="1.5">
                <el-button
                    type="warning"
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    v-hasPermi="['biz:edu:export']"
                    >导出</el-button
                >
            </el-col>
        </el-row>

        <el-table
            style="width: 100%"
            v-loading="loading"
            :data="eduList"
            @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" width="55" align="center" />

            <el-table-column label="申请金额" align="center" prop="fee" />
            <el-table-column label="授信额度" align="center" prop="sfee" />

            <el-table-column
                label="创建时间"
                align="center"
                prop="createdTime"
                width="200"
            >
                <template slot-scope="scope">
                    <span>{{
                        parseTime(
                            scope.row.createdTime,
                            "{y}-{m}-{d} {h}:{i}:{s}"
                        )
                    }}</span>
                </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="status" />

            <!--<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['biz:edu:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['biz:edu:remove']"
          >删除</el-button>
        </template>
      </el-table-column>-->
        </el-table>

        <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 添加或修改申请额度记录对话框 -->
        <el-dialog
            :title="title"
            :visible.sync="open"
            width="500px"
            append-to-body
        >
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="申请金额" prop="fee">
                    <el-input v-model="form.fee" onkeyup="this.value=this.value.replace(/[^A-Z0-9]/g,'');" placeholder="请输入申请金额" />
                </el-form-item>

                <el-form-item label="资质文件" prop="file" required>
                    <el-upload
                        ref="file"
                        :headers="headers"
                        :on-success="operateUploadSuccess"
                        :file-list="form.file"
                        :action="uploadImgUrl"
                        :before-upload="fieldBeforeUpload"
                        :on-remove="operateRemove"
                        list-type="picture-card"
                    >
                        <i class="el-icon-plus"></i>
                    </el-upload>
                </el-form-item>

                <el-form-item label="备注" prop="msg">
                    <el-input v-model="form.msg" placeholder="请输入备注" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listEdu,
    getEdu,
    delEdu,
    addEdu,
    updateEdu,
    exportEdu
} from "@/api/biz/edu";
import { getToken } from "@/utils/auth";
export default {
    name: "Edu",
    data() {
        return {
            isshow: true,
            userinfo: {},
            disabled: true,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // 申请额度记录表格数据
            eduList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                udi: undefined,
                fee: undefined,
                file: undefined,
                status: undefined,
                createdTime: undefined,
                sfee: undefined,
                msg: undefined
            },

            uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
            headers: {
                Authorization: "Bearer " + getToken()
            },

            typeoptions: [
                {
                    value: "0",
                    label: "待审核"
                },
                {
                    value: "1",
                    label: "审核通过"
                },
                {
                    value: "2",
                    label: "审核不通过"
                },
                {
                    value: "3",
                    label: "授信成功"
                }
            ],

            fileList: [],

            // 表单参数
            form: {},
            // 表单校验
            rules: {
                fee: [
                    {
                        required: true,
                        message: "申请金额不能为空",
                        trigger: "blur"
                    }
                ],
                file: [
                    {
                        required: true,
                        message: "资质文件不能为空",
                        trigger: "blur"
                    }
                ]
            }
        };
    },
    created() {
        this.getUrl("/web/getCurrectUserinfo").then(response => {
            this.userinfo = response.data;
            // console.log(response.settype);
            if (response.settype == 0) {
                this.isshow = false;
            } else {
                this.isshow = true;
            }
        });
        this.getList();
    },
    methods: {
        operateUploadSuccess(response) {
            if (response.code == 200) {
                this.fileList.push(response.fileName);
            }
        },
        operateRemove(obj) {
            var tmpimg = obj.url.substr(8);
            this.fileList.forEach((item, index) => {
                if (item == tmpimg) {
                    this.fileList.splice(index, 1);
                }
            });
        },

        fieldBeforeUpload(file) {
            let isRightSize = file.size / 1024 / 1024 < 2;
            if (!isRightSize) {
                this.$message.error("文件大小超过 2MB");
            }
            return isRightSize;
        },

        /** 查询申请额度记录列表 */
        getList() {
            this.loading = true;
            listEdu(this.queryParams, 0).then(response => {
                this.eduList = response.rows;
                this.eduList.forEach((item, index) => {
                    if (item.status == 0) {
                        this.eduList[index].status = "待审核";
                        this.eduList[index].sfee = "-";
                    } else if (item.status == 1) {
                        this.eduList[index].status = "审核通过";
                    } else if (item.status == 2) {
                        this.eduList[index].status = "审核不通过";
                    } else if (item.status == 3) {
                        this.eduList[index].status = "授信成功";
                    }

                });
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                udi: undefined,
                fee: undefined,
                file: undefined,
                status: "0",
                createdTime: undefined,
                sfee: undefined,
                msg: undefined
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id);
            this.single = selection.length != 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加申请额度记录";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getEdu(id).then(response => {
                this.form = response.data;

                var images = this.form.file.split(",");
                var tmp = [];
                images.forEach(item => {
                    if (item != "") {
                        tmp.push({
                            name: "cdscs",
                            url: process.env.VUE_APP_BASE_API + item
                        });
                        this.fileList.push(item);
                    }
                });
                this.form.file = tmp;

                this.open = true;
                this.title = "修改申请额度记录";
            });
        },
        /** 提交按钮 */
        submitForm: function() {
            if (this.form.fee <= 0) {
                this.msgError("申请金额错误");
                return;
            }
            this.form.file = this.fileList.join(",");

            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != undefined) {
                        updateEdu(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    } else {
                        addEdu(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$confirm(
                '是否确认删除申请额度记录编号为"' + ids + '"的数据项?',
                "警告",
                {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }
            )
                .then(function() {
                    return delEdu(ids);
                })
                .then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                })
                .catch(function() {});
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$confirm("是否确认导出申请额度记录数据项?", "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(function() {
                    return exportEdu(queryParams);
                })
                .then(response => {
                    //this.download(response.msg);
                    this.msgSuccess(response.msg);
                })
                .catch(function() {});
        }
    }
};
</script>
<style rel="stylesheet/scss" lang="scss">
    .sedu .el-form-item {
        margin-bottom: 22px;
    }
</style>
